package cn.cqie.attendance.student.controller;


import cn.cqie.attendance.common.util.Page;
import cn.cqie.attendance.student.domian.*;
import cn.cqie.attendance.student.mapper.StudentMapper;
import cn.cqie.attendance.student.sevice.IStudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 学生信息Controller
 * @author xhd
 * @date 2021-06-18
 */
@Api(tags = "学生模块")
@RestController
@RequestMapping("/student")
public class StudentController
{

    @Autowired
    private IStudentService studentService;

    @Resource
    private StudentMapper studentMapper;


    /*系统当前登陆用户Id*/
  // private Long stuId = CurrentInfoUtil.getUserId().longValue();

    /*上传文件服务器路径*/
    @Value("${attachmentFilePath}")
    String path;

    /**
     * 学生基本信息查询
     * @param stuId
     * @return
     */
    @ApiOperation("学生基本信息查询")
    @GetMapping("/queryStudentById")
    @ApiImplicitParams(
            @ApiImplicitParam(name="stuId",value = "学生Id（1）")
    )
    public Page<Student> queryStudentById(
            @RequestParam("stuId") Long stuId
    )
    {
       Student student = studentService.queryStudentById(stuId);
        List<Student> studentList=new ArrayList<>();
        studentList.add(student);
       return new Page(200,"success",studentList);
    }

    /**
     * 学生实训小组列表查询
     * @param stuId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation("学生实训小组列表查询")
    @GetMapping("/queryStudentList")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name="stuId",value = "学生Id（1）"),
                    @ApiImplicitParam(name="pageNum",value = "第几页（1）"),
                    @ApiImplicitParam(name="pageSize",value = "每页记录数（5）")
            }
    )
    public Page<ResultStudent> queryStudentList(
            @RequestParam("stuId") Long stuId,
            @RequestParam("pageNum")int pageNum,
            @RequestParam("pageSize")int pageSize)
    {
        PageHelper.startPage(pageNum,pageSize);
        List<ResultStudent> studentList = studentService.queryStudentList(stuId);
        studentList.forEach(student -> System.out.println("-------------"+student.getStuName()));
        PageInfo pageInfo = new PageInfo(studentList);
        return new Page(200,"success",pageInfo.getPageNum(),pageInfo.getPageSize(),pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 学生实训成绩列表查询
     * @param pageNum
     * @param stuId
     * @param pageSize
     * @return
     */
    @ApiOperation("学生实训成绩列表查询")
    @GetMapping("/queryStudentGroupItemScore")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name="stuId",value = "学生Id（1）"),
                    @ApiImplicitParam(name="pageNum",value = "第几页（1）"),
                    @ApiImplicitParam(name="pageSize",value = "每页记录数（5）")
            }
    )
    public Page<ResultScore> queryStudentGroupItemScore(
            @RequestParam("stuId") Long stuId,
            @RequestParam("pageNum")int pageNum,
            @RequestParam("pageSize")int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<ResultScore> groupScoreList=studentService.queryStudentGroupItemScore(stuId);
        PageInfo pageInfo = new PageInfo(groupScoreList);
        return new Page(200,"success",pageInfo.getPageNum(),pageInfo.getPageSize(),pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 学生实训小组成绩列表排行查询
     * @param courseId
     * @return
     */
    @ApiOperation("学生实训小组成绩列表排行查询")
    @GetMapping("/queryStudentGroupItemScoreRankNo")
    @ApiImplicitParam(
            name="courseId",value = "课程Id（1）"
    )
    public Page<Student> queryStudentGroupItemScoreRankNo(@RequestParam("courseId")Long courseId){
        List<Student> groupScoreList=studentService.queryStudentGroupItemScoreRankNo(courseId);
        return new Page(200,"success",groupScoreList);
    }

    /**
     * 修改学生邮箱
     * @param stuId
     * @param email
     * @return
     */
    @ApiOperation("修改学生邮箱")
    @PostMapping("/updateEmail")
    @ApiImplicitParam(name="stuId",value = "学生Id（1）")
    public Page updateEmail(
            @RequestParam("stuId") Long stuId,
            @RequestParam("email") String email){
        int row=studentService.updateEmail(stuId,email);
        if(row>0){
            return Page.SUCCESS;
        }
       return Page.FAIL;
    }

    /**
     * 修改学生电话号码
     * @param stuId
     * @param phone
     * @return
     */
    @ApiOperation("修改学生电话号码")
    @PostMapping("/updatePhoneNumber")
    @ApiImplicitParam(name="stuId",value = "学生Id（1）")
    public Page updatePhoneNumber(
            @RequestParam("stuId") Long stuId,
            @RequestParam("phone") String phone){
        int row = studentService.updatePhoneNumber(stuId,phone);
        if(row>0){
            return Page.SUCCESS;
        }
        return Page.FAIL;
    }

    /**
     * 学生上传头像
     * @param file
     * @param stuId
     * @return
     * */
    @ApiOperation("学生头像上传")
    @PostMapping("/uploadStudentFace")
    @ApiImplicitParams({
            @ApiImplicitParam(name="stuId",value = "学生Id"),
            @ApiImplicitParam(name="file",value = "上传文件")
    })
    public Page uploadStudentFace(
            @RequestParam("stuId") Long stuId,
            @RequestParam("file") MultipartFile file){
      File f = new File(this.path);
      if(!f.exists()){
          f.mkdirs();
      }
        // 上传简单文件名
        String originalFilename = file.getOriginalFilename();
        //存储路径
        String filePath = new StringBuilder(this.path)
                .append(System.currentTimeMillis())
                .append(originalFilename)
                .toString();
        try {
            // 保存文件
            file.transferTo(new File(filePath));
            if(studentService.uploadStudentFace(stuId,filePath)){
                return Page.SUCCESS;
            }else{
               return Page.FAIL;
            }
        } catch (IOException e) {
            e.printStackTrace();
           return Page.EXCEPTION;
        }
    }

    /**
     * 条件查询学生实训课程信息
     * @param course
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation("条件查询学生实训课程信息")
    @GetMapping("/queryStudentCourseByInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(name="courseName",value = "课程名称(java基础实训)"),
            @ApiImplicitParam(name="endTime",value = "开始时间"),
            @ApiImplicitParam(name="pageNum",value = "第几页(1)"),
            @ApiImplicitParam(name="pageSize",value = "每页记录数(5)"),
            @ApiImplicitParam(name = "responseteacher",value = "责任老师Id"),
            @ApiImplicitParam(name="startTime",value = "开始时间"),
            @ApiImplicitParam(name="trainteacherName",value = "实训老师")
    })
    public Page<Course> queryStudentCourseByInfo(QueryCourse course, @RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Course> courseList=studentService.queryStudentCourseByInfo(course);
        PageInfo pageInfo = new PageInfo(courseList);
        return new Page(200,"success",pageInfo.getPageNum(),pageInfo.getPageSize(),pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 条件查询学生实训作业信息列表
     * @param homework
     * @return
     */
    @ApiOperation("条件查询学生实训作业信息列表")
    @GetMapping("/queryStudentTaskByInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(name="completeTime",value = "完成时间"),
            @ApiImplicitParam(name="courseName",value = "课程名称（Java基础实训）"),
            @ApiImplicitParam(name="endTime",value = "结束时间"),
            @ApiImplicitParam(name="homeworkTheme",value = "作业主题"),
            @ApiImplicitParam(name="homeworkType",value = "作业类型（0 平时作业 1  阶段作业 2 最终作业）"),
            @ApiImplicitParam(name="startTime",value = "开始时间"),
            @ApiImplicitParam(name="status",value = "作业状态（0 未提交 1 待批复 2 已批复）"),
            @ApiImplicitParam(name="stuId",value = "学生Id"),
            @ApiImplicitParam(name="trainteacherName",value = "实训老师姓名")
    })
    public Page<ResultHomework> queryStudentTaskByInfo(QueryHomework homework){
        List<ResultHomework> homeworkList= studentService.queryStudentTaskByInfo(homework);
        return new Page(200,"success",homeworkList);
    }

    /**
     * 学生实训作业内容查询
     * @param homId 作业Id
     * @param phId 发布作业Id
     * @return
     */
     @ApiOperation("学生实训作业内容查询")
     @GetMapping("/queryStudentTaskInfoById")
     @ApiImplicitParams({
             @ApiImplicitParam(name="phId",value = "发布作业Id（1）"),
             @ApiImplicitParam(name="homId",value = "作业Id（1）")
     })
     public Page<ResultHomework> queryStudentTaskInfoById(@RequestParam("phId") Long phId, @RequestParam("homId")Long homId){
         List<ResultHomework> PublishHomework=studentService.queryStudentTaskInfoById(phId,homId);
        return new Page(200,"success",PublishHomework);
     }

    /**
     * 学生答题
     * @param phId
     * @param homId
     * @param answerContent
     * @param files
     * @param homeworkType
     * @param status
     * @return
     * */
    @ApiOperation("学生答题（多文件上传+富文本）+暂存草稿")
    @PostMapping("/homworkSubmit")
    @ApiImplicitParams({
            @ApiImplicitParam(name="homeworkType",value = "作业类型（0 平时作业 1  阶段作业 2 最终作业）"),
            @ApiImplicitParam(name="status",value = "作业状态（0 未提交 1 待批复 2 已批复）"),
            @ApiImplicitParam(name="phId",value = "发布作业Id（1）"),
            @ApiImplicitParam(name="homId",value = "作业Id（1）")
    })
    public Page homworkSubmit(MultipartFile[] files,
                              String answerContent,
                              @RequestParam("homeworkType") int homeworkType,
                              @RequestParam("status")int status,
                              @RequestParam("phId")Long phId,
                              @RequestParam("homId")Long homId){
        int row=0;
        if(files.length>0){
            File f = new File(this.path);
            if(!f.exists()){
                f.mkdirs();//创建目录
                System.out.println("测试文件夹不存在");
            }
            for (MultipartFile file : files){
                String originalFilename = file.getOriginalFilename(); //获取文件文件名
                // 存储路径
               String  filePath = new StringBuilder(this.path)
                .append(System.currentTimeMillis())
                .append(originalFilename)
                .toString();
                 //保存文件
                try{
                     file.transferTo(new File(filePath));
                  row  = studentService.insertAttachment(filePath,homeworkType,phId,homId);
                }catch (Exception e){
                    e.printStackTrace();
                    return Page.FAIL;
                }
            }
        }
        if(row>0){
            studentService.updateHomework(phId,homId,answerContent,status);
            return Page.SUCCESS;
        }
       return Page.FAIL;
    }

    /**
     * 草稿内容查询
     * @param homId
     * @param status
     * @param phId
     * @return
     * */
    @ApiOperation("草稿内容查询")
    @GetMapping("/queryDraftInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(name="status",value = "作业状态（0 未提交 1 待批复 2 已批复）0"),
            @ApiImplicitParam(name="phId",value = "发布作业Id（2）"),
            @ApiImplicitParam(name="homId",value = "作业Id（5）"),
    })
    public Page<ResultHomework> queryDraftInfo( @RequestParam("status")int status,
                                                @RequestParam("phId")Long phId,
                                                @RequestParam("homId")Long homId){
        ResultHomework resultHomework=studentService.queryDraftInfo(status,phId,homId);
        List<ResultHomework> resultHomeworkList = new ArrayList<>();
        resultHomeworkList.add(resultHomework);
        return new Page(200,"success",resultHomeworkList);
    }



    /**
     * 查询作业批复情况
     * @param homId
     * @param phId
     * @param stuId
     * @return
     * */
    @ApiOperation("学生查询作业批复查询")
    @GetMapping("/queryHomeworkApproval")
    @ApiImplicitParams({
            @ApiImplicitParam(name="phId",value = "发布作业Id（1）"),
            @ApiImplicitParam(name="homId",value = "作业Id（1）"),
            @ApiImplicitParam(name="stuId",value = "学生Id（1）")
    })
     public Page<Homework> queryHomeworkApproval(@RequestParam("phId") Long phId,
                                                 @RequestParam("homId") Long homId,
                                                 @RequestParam("stuId") Long stuId
    ){
        Homework homework=studentService.queryHomeworkApproval(phId,homId,stuId);
        List<Homework> homeworkList = new ArrayList<>();
        homeworkList.add(homework);
        return new Page(200,"success", homeworkList);
    }

     /**
      * 查询学生的考勤记录
      * @param stuId
      * @param pageSize
      * @param pageNum
      * @return
      * */
     @ApiOperation("学生考勤记录查询")
     @GetMapping("/queryStudentAttendance")
     @ApiImplicitParams({
             @ApiImplicitParam(name="stuId",value = "学生Id（1）"),
             @ApiImplicitParam(name="pageNum",value = "第几页（1）"),
             @ApiImplicitParam(name ="pageSize",value = "每页记录数（5）")
     })
    public Page<Attendance> queryStudentAttendance(
            @RequestParam("stuId") Long stuId,
            @RequestParam("pageNum")int pageNum,
            @RequestParam("pageSize")int pageSize){
         PageHelper.startPage(pageNum,pageSize);
       List<ResultAttendance> attendances= studentService.queryStudentAttendance(stuId);
       PageInfo pageInfo = new PageInfo(attendances);
       return new Page(200,"success",pageInfo.getPageNum(),pageInfo.getPageSize(),pageInfo.getTotal(),pageInfo.getList());
     }

    /**
     * 学生的考勤记录查询
     * @param pageNum
     * @param pageSize
     * @param stuId
     * @return
     * */
     @ApiOperation("学生打卡记录查询")
    @GetMapping("/queryStudentAttendanceRecord")
     @ApiImplicitParams({
             @ApiImplicitParam(name="stuId",value = "学生Id（1）"),
             @ApiImplicitParam(name="pageNum",value = "第几页（1）"),
             @ApiImplicitParam(name ="pageSize",value = "每页记录数（5）")
     })
    public Page<ResultAttendance> queryStudentAttendanceRecord(
                                             @RequestParam("stuId") Long stuId,
                                             @RequestParam("pageNum")int pageNum,
                                             @RequestParam("pageSize")int pageSize){
         PageHelper.startPage(pageNum,pageSize);
         List<ResultAttendance> attendances= studentService.queryStudentAttendanceRecord(stuId);
         PageInfo pageInfo = new PageInfo(attendances);
         return new Page(200,"success",pageInfo.getPageNum(),pageInfo.getPageSize(),pageInfo.getTotal(),pageInfo.getList());
     }

     /**
      * 下拉框数据查询，通过学生id查询该学生所有的课程
      * @param stuId
      * @return
      * */
     @ApiOperation("下拉框-项目名称")
     @GetMapping("/queryStudentCourseById")
     @ApiImplicitParam(name = "stuId",value = "学生Id(1)")
     public Page<Course> queryStudentCourseById(
             @RequestParam("stuId") Long stuId
     ) {
         List<Course> courseList = studentService.queryStudentCourseById(stuId);
         return new Page(200, "success", courseList);
     }


    /**
     * 下拉框数据查询，通过学生id查询该学生实训老师
     * @param stuId
     * @return
     * */
    @ApiOperation("下拉框-实训老师")
    @GetMapping("/queryStudentTrainteacherById")
    @ApiImplicitParam(name = "stuId",value = "学生Id(1)")
    public Page<TrainteacherCourse> queryStudentTrainteacherById(
            @RequestParam("stuId") Long stuId
    ){
        List<TrainteacherCourse> trainteacherCourseList = studentService.queryStudentTrainteacherById(stuId);
        return new Page(200,"success",trainteacherCourseList);
    }

    /**
     * 下拉框数据查询，通过学生id查询该学生责任老师
     * @param stuId
     * @return
     * */
    @ApiOperation("下拉框-责任老师")
    @GetMapping("/queryStudentResponseteacherById")
    @ApiImplicitParam(name = "stuId",value = "学生Id(1)")
    public Page<Course> queryStudentResponseteacherById(
            @RequestParam("stuId") Long stuId
    ){
        List<Course> courseList = studentService.queryStudentResponseteacherById(stuId);
        return new Page(200,"success",courseList);
    }




}
